package cn.sumpu.app.guoguojie.biz.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import cn.sumpu.app.guoguojie.biz.AlbumDoingBiz;
import cn.sumpu.app.guoguojie.biz.IdentifierBiz;
import cn.sumpu.app.guoguojie.biz.PictorialTopicBiz;
import cn.sumpu.app.guoguojie.dao.AlbumDao;
import cn.sumpu.app.guoguojie.domain.Album;
import cn.sumpu.app.guoguojie.domain.AlbumDoing;
import cn.sumpu.app.guoguojie.domain.Doing;
import cn.sumpu.app.guoguojie.domain.DoingPictorial;
import cn.sumpu.app.guoguojie.domain.PictorialDoing;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;
import cn.sumpu.app.guoguojie.util.Common;
import cn.sumpu.app.guoguojie.util.PageModel;

/**
 * 
 * @author grieous
 * @version $Id: AlbumDoingBizImpl.java, v 0.1 2012-9-21 下午03:16:11 grieous Exp $
 */
public class AlbumDoingBizImpl implements AlbumDoingBiz {
    private AlbumDao albumDao;

    public void setAlbumDao(AlbumDao albumDao) {
        this.albumDao = albumDao;
    }

    @Resource
    private IdentifierBiz     identifierBiz;
    @Autowired
    private PictorialTopicBiz topicBiz;

    @Override
    public void saveAlbum(Album album) throws AppBusinessException {
        if (!Common.empty(album)) {
            if (Common.empty(album.getIdentifier())) {
                String identifier = identifierBiz.generateId("album");
                album.setIdentifier(identifier);
                int min = getAlbumMinSort();
                album.setSort(min - 1);
                albumDao.insertAlbum(album);
            } else {
                albumDao.updateAlbum(album);
            }
        }
    }

    @Override
    public void addAlbumDoing(AlbumDoing albumDoing) throws AppBusinessException {
        if (!Common.empty(albumDoing)) {
            int min = getAlbumDoingMinSort(albumDoing.getAlbum());
            albumDoing.setSort(min - 1);
            albumDao.insertAlbumDoing(albumDoing);
        }
    }

    @Override
    public void updateAlbum(Album album) throws AppBusinessException {
    }

    @Override
    public void updateAlbumDoing(AlbumDoing albumDoing) throws AppBusinessException {
    }

    @Transactional(rollbackForClassName = { "cn.sumpu.app.guoguojie.exception.AppBusinessException" }, timeout = 10)
    @Override
    public void deleteAlbum(String albumId) throws AppBusinessException {
        Map<String, Object> map1 = new HashMap<String, Object>();
        map1.put("identifier", albumId);
        albumDao.deleteAlbum(map1);
        Map<String, Object> map2 = new HashMap<String, Object>();
        map1.put("album", albumId);
        albumDao.deleteAlbumDoing(map2);
    }

    @Override
    public void deleteAlbumDoing(String album, String doing) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("album", album);
        map.put("doing", doing);
        albumDao.deleteAlbumDoing(map);
    }

    @Override
    public Album getAlbumById(String albumId) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("identifier", albumId);
        List<Album> list = albumDao.getAlbum(map);
        if (list != null && list.size() == 1) {
            return list.get(0);
        }
        return null;
    }

    @Transactional(rollbackForClassName = { "cn.sumpu.app.guoguojie.exception.AppBusinessException" }, timeout = 10)
    @Override
    public PageModel getAlbumList(Integer page, Integer pageSize, String title, Boolean relationDoing) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        page = page == null ? 1 : page;
        int size = pageSize == null ? 10 : pageSize;
        map.put("startId", (page - 1) * size);
        map.put("pageSize", size);
        if (!Common.empty(title)) {
            map.put("title", "%" + title + "%");
        }
        int count = albumDao.getAlbumCount(map);
        PageModel pm = new PageModel();
        pm.setCurrentPage(page);
        List<Album> list = null;
        if (relationDoing) {
            list = albumDao.getAlbumRelationDoing(map);
        } else {
            list = albumDao.getAlbum(map);
        }
        pm.setList(list);
        pm.setPageSize(size);
        pm.setTotalRecord(count);
        pm.setTotalPage((count + size - 1) / size);
        return pm;
    }

    @Override
    public PageModel getAlbumDoingByAlbum(Integer page, Integer pageSize, String albumId) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        page = page == null ? 1 : page;
        int size = pageSize == null ? 10 : pageSize;
        map.put("startId", (page - 1) * size);
        map.put("pageSize", size);
        map.put("album", albumId);
        int count = albumDao.getAlbumDoingCount(map);
        PageModel pm = new PageModel();
        pm.setCurrentPage(page);
        List<AlbumDoing> list = albumDao.getAlbumDoing(map);
        pm.setList(list);
        pm.setPageSize(size);
        pm.setTotalRecord(count);
        pm.setTotalPage((count + size - 1) / size);
        return pm;
    }

    @Override
    public Album getAlbumBySort(Integer sort) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("sort", sort);
        List<Album> list = albumDao.getAlbum(map);
        if (list != null && list.size() == 1) {
            return list.get(0);
        }
        return null;
    }

    @Transactional(rollbackForClassName = { "cn.sumpu.app.guoguojie.exception.AppBusinessException" }, timeout = 10)
    @Override
    public void moveAlbumSortUp(String albumId) throws AppBusinessException {
        Album downAlbum = getAlbumById(albumId);
        if (!Common.empty(downAlbum)) {
            int downSort = downAlbum.getSort();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("moveSort", downSort);
            int upSort = albumDao.getAlbumMaxSort(map);
            Album upAlbum = getAlbumBySort(upSort);
            if (!Common.empty(upAlbum)) {
                changeAlubmSort(downAlbum, upAlbum);
            }
        }
    }

    @Override
    public void moveAlbumSortDown(String albumId) throws AppBusinessException {
        Album upAlbum = getAlbumById(albumId);
        if (!Common.empty(upAlbum)) {
            int upSort = upAlbum.getSort();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("moveSort", upSort);
            int downSort = albumDao.getAlbumMinSort(map);
            Album downAlbum = getAlbumBySort(downSort);
            if (!Common.empty(downAlbum)) {
                changeAlubmSort(upAlbum, downAlbum);
            }
        }
    }

    @Override
    public void moveAlbumSortTop(String albumId) throws AppBusinessException {
        Album moveAlbum = getAlbumById(albumId);
        if (!Common.empty(moveAlbum)) {
            int minSort = getAlbumMinSort();
            Album topAlbum = getAlbumBySort(minSort);
            if (!Common.empty(topAlbum)) {
                changeAlubmSort(moveAlbum, topAlbum);
            }
        }
    }

    @Transactional(rollbackForClassName = { "cn.sumpu.app.guoguojie.exception.AppBusinessException" }, timeout = 10)
    @Override
    public void changeAlubmSort(Album album1, Album album2) throws AppBusinessException {
        if (!Common.empty(album1) && !Common.empty(album2)) {
            int sort1 = album1.getSort();
            int sort2 = album2.getSort();
            album1.setSort(sort2);
            album2.setSort(sort1);
            albumDao.updateAlbum(album1);
            albumDao.updateAlbum(album2);
        }

    }

    @Override
    public int getAlbumMinSort() throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        return albumDao.getAlbumMinSort(map);

    }

    @Override
    public AlbumDoing getAlbumDoingById(String albumId, String doingId) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("album", albumId);
        map.put("doing", doingId);
        List<AlbumDoing> list = albumDao.getAlbumDoing(map);
        if (list != null && list.size() == 1) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public AlbumDoing getAlbumDoingBySort(String albumId, Integer sort) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("sort", sort);
        map.put("album", albumId);
        List<AlbumDoing> list = albumDao.getAlbumDoing(map);
        if (list != null && list.size() == 1) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public void moveAlbumDoingSortUp(String albumId, String doingId) throws AppBusinessException {
        AlbumDoing downAlbumDoing = getAlbumDoingById(albumId, doingId);
        if (!Common.empty(downAlbumDoing)) {
            int downSort = downAlbumDoing.getSort();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("moveSort", downSort);
            int upSort = albumDao.getAlbumDoingMaxSort(map);
            AlbumDoing upAlbumDoing = getAlbumDoingBySort(albumId, upSort);
            if (!Common.empty(upAlbumDoing)) {
                changeAlubmDoingSort(downAlbumDoing, upAlbumDoing);
            }
        }
    }

    @Override
    public void moveAlbumDoingSortDown(String albumId, String doingId) throws AppBusinessException {
        AlbumDoing upAlbumDoing = getAlbumDoingById(albumId, doingId);
        if (!Common.empty(upAlbumDoing)) {
            int upSort = upAlbumDoing.getSort();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("moveSort", upSort);
            int downSort = albumDao.getAlbumDoingMinSort(map);
            AlbumDoing downAlbumDoing = getAlbumDoingBySort(albumId, downSort);
            if (!Common.empty(downAlbumDoing)) {
                changeAlubmDoingSort(upAlbumDoing, downAlbumDoing);
            }
        }
    }

    @Override
    public void moveAlbumDoingSortTop(String albumId, String doingId) throws AppBusinessException {
        AlbumDoing moveAlbumDoing = getAlbumDoingById(albumId, doingId);
        if (!Common.empty(moveAlbumDoing)) {
            int minSort = getAlbumDoingMinSort(albumId);
            AlbumDoing topAlbumDoing = getAlbumDoingBySort(albumId, minSort);
            if (!Common.empty(topAlbumDoing)) {
                changeAlubmDoingSort(moveAlbumDoing, topAlbumDoing);
            }
        }
    }

    @Transactional(rollbackForClassName = { "cn.sumpu.app.guoguojie.exception.AppBusinessException" }, timeout = 10)
    @Override
    public void changeAlubmDoingSort(AlbumDoing albumDoing1, AlbumDoing albumDoing2) throws AppBusinessException {
        if (!Common.empty(albumDoing1) && !Common.empty(albumDoing2)) {
            int sort1 = albumDoing1.getSort();
            int sort2 = albumDoing2.getSort();
            albumDoing1.setSort(sort2);
            albumDoing2.setSort(sort1);
            albumDao.updateAlbumDoing(albumDoing1);
            albumDao.updateAlbumDoing(albumDoing2);
        }
    }

    @Override
    public int getAlbumDoingMinSort(String albumId) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("album", albumId);
        return albumDao.getAlbumDoingMinSort(map);
    }

    @Override
    public List<Album> getAllAlbum() throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        return albumDao.getAlbum(map);
    }

    @Transactional(rollbackForClassName = { "cn.sumpu.app.guoguojie.exception.AppBusinessException" }, timeout = 10)
    @Override
    public String pustToAlbum(String pictorialIds, String doingIds) throws AppBusinessException {
        String pictorialIdArr[] = pictorialIds.split(",");
        String doingIdArr[] = doingIds.split(",");
        for (int j = 0; j < pictorialIdArr.length; j++) {
            String pictorialId = pictorialIdArr[j];
            if (!Common.empty(pictorialId)) {
                DoingPictorial doingPictorial = topicBiz.getPictorialById(pictorialId);
                if (Common.empty(doingPictorial)) {
                    return "画报" + pictorialId + "不存在";
                }
                for (int i = 0; i < doingIdArr.length; i++) {
                    String doingId = doingIdArr[i];
                    if (!Common.empty(doingId)) {
                        Doing doing = getDoingById(doingId);
                        if (Common.empty(doing)) {
                            return "微博" + doingId + "不存在";
                        }
                        PictorialDoing picDoing = topicBiz.getPictorialDoingById(pictorialId, doingId);
                        if (Common.empty(picDoing)) {
                            picDoing = new PictorialDoing();
                            picDoing.setPictorial(pictorialId);
                            picDoing.setDoing(doingId);
                            topicBiz.savePictorialDoing(picDoing);
                        }

                    }
                }
            }
        }
        return "success";

    }

    @Override
    public Doing getDoingById(String doingId) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("identifier", doingId);
        List<Doing> list = albumDao.getDoing(map);
        if (list != null && list.size() == 1) {
            return list.get(0);
        }
        return null;
    }
}
